package cn.edu.zafu.bigdata.bd121.studentdb.command.impl;

import cn.edu.zafu.bigdata.bd121.studentdb.cli.CliMain;
import cn.edu.zafu.bigdata.bd121.studentdb.command.BaseCommand;
import cn.edu.zafu.bigdata.bd121.studentdb.entity.StudentPO;
import cn.edu.zafu.bigdata.bd121.studentdb.utils.InputUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 插入学生信息命令
 *
 * @author 202211090121
 */
public class InsertStudent extends BaseCommand {

    /**
     * 处理用户输入
     */
    @Override
    protected void handleUserInput() {
        Long id = Long.parseLong(InputUtils.getValidId(true));
        String name = InputUtils.getValidName();
        String phone = InputUtils.getValidPhone();
        try {
            PreparedStatement statement = CliMain.connection.prepareStatement("INSERT INTO student121(ID, NAME, PHONE) VALUES(?,?,?) ");
            statement.setLong(1, id);
            statement.setString(2, name);
            statement.setString(3, phone);
            statement.executeUpdate();
        } catch (SQLException e) {
            System.err.println("数据库异常");
            throw new RuntimeException(e);
        }
        System.out.println("学生信息添加成功！");
    }

}
